
const app = getApp()
Page({
  data: {
    userId: null,
    skillArr1: ['自行车'],
    skillArr2: null,
    skillArr3: null,
    skillSelected: null,
    wh:0
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onShow: function () {
    if (app.globalData.userId) {
      var that = this;
      this.setData({
        userId: app.globalData.userId
      })
      wx.request({
        url: 'https://www.xinhuo.site/api/skill/edit',
        data: {
          userId: this.data.userId
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          if (res.data.syscode == 0) {
            that.data.skillSelected = res.data.recordList[0].skillId;
            that.data.skillArr1 = res.data.skillList1;
            that.data.skillArr2 = res.data.skillList2;
            that.data.skillArr3 = res.data.skillList3;
            that.setData({
              skillSelected: that.data.skillSelected,
              skillArr1: that.data.skillArr1,
              skillArr2: that.data.skillArr2,
              skillArr3: that.data.skillArr3
            })
          }
        }
      })
    }
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  skillChange: function(e) {
    var thisId = e.target.dataset.id
    var skillSelected = this.data.skillSelected
    if (skillSelected.indexOf(thisId) >= 0) {
      skillSelected = skillSelected.replace(thisId, '')
    }
    else {
      skillSelected = skillSelected + ',' + thisId
    }
    this.setData({
      skillSelected: skillSelected.replace(',,', ',')
    });
  },
  toSave(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 5000
    })
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/skill/save',
      data: {
        userId: that.data.userId,
        skillIds: that.data.skillSelected
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        wx.hideToast();
        if (res.data.syscode == 0) {
          wx.showToast({
            title: '提交成功',
            duration: 1000
          })
          wx.navigateBack({
            delta: 1
          });
        }
        else {
          wx.showToast({
            title: '操作失败',
            duration: 3000
          })
        }
      },
      fail(res) {
        wx.hideToast();
        wx.showToast({
          title: '操作失败，请检查网络',
          duration: 3000
        })
      }
    })
  }
})
